import { defineStore } from 'pinia'
import * as roleApi from '../../../api/system/role'
import type { Role } from '../../../types/app.d'

// 角色管理 Store 接口
interface RoleManagementState {
  roleList: Role[]
  total: number
  loading: boolean
  currentRole: Role | null
  roleMenus: string[]
}

export const useRoleStore = defineStore('roleManagement', {
  state: (): RoleManagementState => ({
    roleList: [],
    total: 0,
    loading: false,
    currentRole: null,
    roleMenus: []
  }),

  getters: {
    // 获取角色列表
    getRoleList(): Role[] {
      return this.roleList
    },

    // 获取角色总数
    getRoleTotal(): number {
      return this.total
    },

    // 获取加载状态
    getLoading(): boolean {
      return this.loading
    },

    // 获取当前角色
    getCurrentRole(): Role | null {
      return this.currentRole
    },

    // 获取角色菜单权限
    getRoleMenus(): string[] {
      return this.roleMenus
    }
  },

  actions: {
    // 获取角色列表
    async getRoleListAction(params: {
      page: number
      pageSize: number
      name?: string
      code?: string
      status?: number
      beginTime?: string
      endTime?: string
    }): Promise<void> {
      this.loading = true
      try {
        const response = await roleApi.getRoleList(params)
        if (response.code === 200) {
          this.roleList = response.data.list || []
          this.total = response.data.total || 0
        }
      } catch (error) {
        console.error('获取角色列表失败:', error)
      } finally {
        this.loading = false
      }
    },

    // 获取角色详情
    async getRoleInfoAction(id: string): Promise<void> {
      try {
        const response = await roleApi.getRoleInfo(id)
        if (response.code === 200) {
          this.currentRole = response.data
        }
      } catch (error) {
        console.error('获取角色详情失败:', error)
      }
    },

    // 创建角色
    async createRoleAction(data: Omit<Role, 'id' | 'createTime' | 'updateTime'>): Promise<boolean> {
      try {
        const response = await roleApi.createRole(data)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('创建角色失败:', error)
        return false
      }
    },

    // 更新角色
    async updateRoleAction(id: string, data: Partial<Omit<Role, 'id' | 'createTime'>>): Promise<boolean> {
      try {
        const response = await roleApi.updateRole(id, data)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('更新角色失败:', error)
        return false
      }
    },

    // 删除角色
    async deleteRoleAction(id: string): Promise<boolean> {
      try {
        const response = await roleApi.deleteRole(id)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('删除角色失败:', error)
        return false
      }
    },

    // 批量删除角色
    async batchDeleteRoleAction(ids: string[]): Promise<boolean> {
      try {
        const response = await roleApi.batchDeleteRole(ids)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('批量删除角色失败:', error)
        return false
      }
    },

    // 修改角色状态
    async changeRoleStatusAction(id: string, status: number): Promise<boolean> {
      try {
        const response = await roleApi.changeRoleStatus(id, status)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('修改角色状态失败:', error)
        return false
      }
    },

    // 获取角色菜单权限
    async getRoleMenusAction(id: string): Promise<void> {
      try {
        const response = await roleApi.getRoleMenus(id)
        if (response.code === 200) {
          this.roleMenus = response.data || []
        }
      } catch (error) {
        console.error('获取角色菜单权限失败:', error)
      }
    },

    // 保存角色菜单权限
    async saveRoleMenusAction(id: string, menuIds: string[]): Promise<boolean> {
      try {
        const response = await roleApi.saveRoleMenus(id, menuIds)
        if (response.code === 200) {
          this.roleMenus = menuIds
          return true
        }
        return false
      } catch (error) {
        console.error('保存角色菜单权限失败:', error)
        return false
      }
    },

    // 清空当前角色和菜单权限
    clearCurrentRole(): void {
      this.currentRole = null
      this.roleMenus = []
    }
  }
})